using System;
using System.Collections;
using System.IO;
using Microsoft.MediaCenter.AddIn;

namespace ScottLane.VideoInbox.AddIn
{
    /// <summary>
    /// Media Center add-in that queues videos for downloading.
    /// </summary>
    public class DownloadAddIn : MarshalByRefObject, IAddInModule, IAddInEntryPoint
    {
        ConfigurationSettings settings;

        public void Initialize(IDictionary dictAppInfo, IDictionary dictEntryPoint)
        {
            settings = ConfigurationSettings.Instance;
        }

        public void Launch(AddInHost host)
        {
            if (host.HostControl.MediaContext != null)
            {
                try
                {
                    string videoFileName = host.HostControl.MediaContext["Name"].ToString();

                    if (Shortcut.IsShortcut(videoFileName))
                    {
                        videoFileName = Shortcut.Load(videoFileName).TargetPath;
                    }

                    string manifestFileName = DownloadManifest.GetFileName(videoFileName);
                    DownloadManifest manifest = DownloadManifest.Load(manifestFileName);
                    int downloadCount = GetDownloadCount(manifest);

                    if (downloadCount > settings.PlaylistDownloadLimit)
                    {
                        if (host.HostControl.Dialog("Are you sure you want to download all " + downloadCount + " videos contained in this playlist?", "Video Inbox", 3, 0, true) == 1)
                        {
                            return;
                        }
                    }

                    DownloadQueue queue = new DownloadQueue();
                    queue.Add(manifest);

                    if (downloadCount > 1)
                    {
                        host.HostControl.DialogNotification(downloadCount + " videos were successfully queued for download.", new int[] { 1 }, settings.NotificationTimeout, null, null);
                    }
                    else
                    {
                        host.HostControl.DialogNotification("Video was successfully queued for download.", new int[] { 1 }, settings.NotificationTimeout, null, null);
                    }
                }
                catch (ArgumentException)
                {
                    host.HostControl.DialogNotification("The selected video cannot be downloaded.", new int[] { 1 }, settings.NotificationTimeout, null, null);
                }
                catch (Exception)
                {
                    // todo - log exception
                    host.HostControl.DialogNotification("An unknown error occurred. Please see the event log for details.", new int[] { 1 }, settings.NotificationTimeout, null, null);
                }
            }
        }

        /// <summary>
        /// Gets a count of the number of videos that need to be downloaded in the specified manifest.
        /// </summary>
        /// <param name="manifest">The download manifest.</param>
        /// <returns>The video count.</returns>
        private int GetDownloadCount(DownloadManifest manifest)
        {
            int downloadCount = 0;

            foreach (Download video in manifest.Downloads)
            {
                if (!video.DestinationExists)
                {
                    downloadCount++;
                }
            }

            return downloadCount;
        }

        public void Uninitialize()
        {

        }
    }
}
